package gbench.mybatis.util;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import gbench.common.fs.XlsFile.DataMatrix;
import gbench.common.fs.XlsFile.SimpleExcel;
import gbench.common.tree.LittleTree.IRecord;

/**
 * 输出工具类
 * 
 * @author xuqinghua
 *
 */
public class Output {

	/**
	 * 行数据输出
	 * 
	 * @param objects 输出的
	 */
	public static String println(final Object... objects) {

		if (null != objects) {
			final var line = Arrays.asList(objects).stream().map(e -> e + "").collect(Collectors.joining("\t"));
			System.out.println(line);
			return line;
		} else {
			System.out.println();
			return null;
		}
	}

	/**
	 * records 转 DataMatrix
	 * 
	 * @param records 数据记录 列表
	 * @return DataMatrix
	 */
	public static DataMatrix<String> recs2mx(final List<IRecord> records) {
		final var array = records.stream().map(e -> e.toArray(s -> s + "")).toArray(String[][]::new);
		final var mx = DataMatrix.of(array).setHeader(records.get(0).keys());
		return mx;
	}

	/**
	 * 节点
	 * 
	 * @param xlsxpath             excel 文件路径
	 * @param lefttop_cell_address 左上角cell的地址名,比如:sheet0!A1
	 * @param records              record 列表
	 */
	public static void writeExcel(final String xlsxpath, final String lefttop_cell_address,
			final List<IRecord> records) {
		final var excel = SimpleExcel.of(xlsxpath);
		excel.write(lefttop_cell_address, Output.recs2mx(records)).save();
		excel.close();
	}

}
